package edu.umd.fosite.graphics;

import javax.media.opengl.GL2;

import edu.umd.fosite.geom.Tree;

public class GLTreeRenderer implements Renderer<GL2, Tree>{

	@Override
	public Drawer<GL2> render(GL2 gl, Tree e) {
		int list = gl.glGenLists(1);
		gl.glNewList(1, GL2.GL_COMPILE);
		drawTree(gl, e);
		gl.glEndList();
		return new GLDisplayListDrawer(gl, list);
	}
	
	private void drawTree(GL2 gl, Tree t) {
		gl.glBegin(GL2.GL_LINES);
		gl.glVertex3d(0, 0, 0);
		gl.glVertex3d(t.direction.X, t.direction.Y, t.direction.Z);
		gl.glEnd();
		
		gl.glPushMatrix();
		gl.glTranslated(t.direction.X, t.direction.Y, t.direction.Z);
		for (Tree p : t.parts)
			drawTree(gl, p);
		gl.glPopMatrix();
	}

}
